//
//  ViewController.swift
//  URLDemo1
//
//  Created by 李煜 on 2024/5/12.
// URL相对路径方式创建

import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //        相对路径创建URL的注意点
        //        NSURL *baseURL = [NSURL URLWithString:@"http://example.com/v1/"];
        //        NSURL *url1 = [NSURL URLWithString:@"foo" relativeToURL:baseURL];// http://example.com/v1/foo
        //        NSURL *url2 = [NSURL URLWithString:@"foo?bar=baz" relativeToURL:baseURL];// http://example.com/v1/foo?bar=baz
        //        NSURL *url3 = [NSURL URLWithString:@"/foo" relativeToURL:baseURL];// http://example.com/foo
        //        NSURL *url4 = [NSURL URLWithString:@"foo/" relativeToURL:baseURL];// http://example.com/v1/foo
        //        NSURL *url5 = [NSURL URLWithString:@"/foo/" relativeToURL:baseURL];// http://example.com/foo/
        //        NSURL *url6 = [NSURL URLWithString:@"http://example2.com/" relativeToURL:baseURL];// http://example2.com/
        
        trailingSlash()
        print()
        trailingWithoutSlash()
    }
    
    // 基准URL结尾有斜杠
    func trailingSlash() {
        //        let baseURL = URL(string: "http://example.com/v1/v2/")!
        let baseURL = URL(string: "http://192.168.5.180:8080/v1/v2/")!
        let u0 = URL(string: "foo", relativeTo: baseURL)! // http://example.com/v1/foo
        let u1 = URL(string: "foo?bar=baz", relativeTo: baseURL)! // http://example.com/v1/foo?bar=baz
        let u2 = URL(string: "/foo", relativeTo: baseURL)! // http://example.com/foo
        let u3 = URL(string: "foo/", relativeTo: baseURL)! // http://example.com/v1/foo
        let u4 = URL(string: "/foo/", relativeTo: baseURL)! // http://example.com/foo/
        let u5 = URL(string: "http://example2.com/", relativeTo: baseURL)! // http://example2.com/
        
        let urls: [URL] = [u0, u1, u2, u3, u4, u5]
        debugPrint("BaseURL: " + baseURL.absoluteString + "")
        for item in urls {
            debugPrint(item.relativeString, item.absoluteString)
        }
    }
    
    // 基准URL结尾无斜杠
    func trailingWithoutSlash() {
        //        let baseURL = URL(string: "http://example.com/v1/v2")!
        let baseURL = URL(string: "http://192.168.5.180:8080/v1/v2")!
        let u0 = URL(string: "foo", relativeTo: baseURL)! // http://example.com/v1/foo
        let u1 = URL(string: "foo?bar=baz", relativeTo: baseURL)! // http://example.com/v1/foo?bar=baz
        let u2 = URL(string: "/foo", relativeTo: baseURL)! // http://example.com/foo
        let u3 = URL(string: "foo/", relativeTo: baseURL)! // http://example.com/v1/foo
        let u4 = URL(string: "/foo/", relativeTo: baseURL)! // http://example.com/foo/
        let u5 = URL(string: "http://example2.com/", relativeTo: baseURL)! // http://example2.com/
        
        let urls: [URL] = [u0, u1, u2, u3, u4, u5]
        debugPrint("BaseURL: " + baseURL.absoluteString + "")
        for item in urls {
            debugPrint(item.relativeString, item.absoluteString)
        }
    }
    
    // 1. 相对路径头带斜杠例如/foo,则直接追加到域名http://example.com 后,不论基准URL结尾带不带/
    // 2. 相对路径头不带斜杠如foo,则直接追加到倒数第一个/处,例子如下:
    // 2.1 若基准URL为http://example.com/v1/v2 则将相对路径foo追加到倒数第一个/处,为http://example.com/v1/foo
    // 2.2 若基准URL为http://example.com/v1/v2/ 则将相对路径foo追加到/处,为http://example.com/v1/v2/foo
}
